/**
 * Copyright 2009-2011 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.myjung.transaction.impl;

import net.myjung.transaction.ITransactionManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @author Steve M. Jung
 */
public class TransactionManager implements ITransactionManager {
	protected final Log logger = LogFactory.getLog(getClass());

	private PlatformTransactionManager transactionManager = null;

	public TransactionStatus getStatus() throws Exception {
		return getStatus(TransactionDefinition.PROPAGATION_REQUIRED, false, TransactionDefinition.ISOLATION_DEFAULT,
				TransactionDefinition.TIMEOUT_DEFAULT);
	}
	public TransactionStatus getStatus(int propagation) throws Exception {
		return getStatus(propagation, false, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.TIMEOUT_DEFAULT);
	}
	public TransactionStatus getStatus(boolean readOnly) throws Exception {
		return getStatus(TransactionDefinition.PROPAGATION_REQUIRED, readOnly, TransactionDefinition.ISOLATION_DEFAULT,
				TransactionDefinition.TIMEOUT_DEFAULT);
	}
	public TransactionStatus getStatus(int propagation, boolean readOnly) throws Exception {
		return getStatus(propagation, readOnly, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.TIMEOUT_DEFAULT);
	}
	public TransactionStatus getStatus(int propagation, boolean readOnly, int isolation) throws Exception {
		return getStatus(propagation, readOnly, isolation, TransactionDefinition.TIMEOUT_DEFAULT);
	}
	public TransactionStatus getStatus(int propagation, boolean readOnly, int isolation, int timeout) throws Exception {
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(propagation);
		def.setReadOnly(readOnly);
		def.setIsolationLevel(isolation);
		def.setTimeout(timeout);
		return getTransactionManager().getTransaction(def);
	}
	public void commit(TransactionStatus status) throws Exception {
		if (status == null)
			return;
		getTransactionManager().commit(status);
	}
	public void rollback(TransactionStatus status) {
		if (status == null)
			return;
		try {
			getTransactionManager().rollback(status);
		} catch (Exception e) {
			logger.warn(e, e);
		}
	}
	public PlatformTransactionManager getTransactionManager() {
		return transactionManager;
	}
	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
}